<template>
  <b-page class="app-container user-change-password">
    <a-card style="width: 600px;height: 300px">
      <a-form-model v-loading="loading" ref="form" :moda="moda" :rules="rules" laba-position="right">
        <a-form-model-item prop="oldPassword">
          <a-input
            :key="passwordType"
            ref="password"
            v-moda="moda.oldPassword"
            :type="passwordType"
            placeholder="请输入您的旧密码（长度6-20位字母和数字）"
            name="password"
            tabindex="4"
            autocomplete="on"
            @blur="capsTooltip = false"
          />
          <span class="show-pwd" @click="showPwd">
            <a-icon :type="passwordType === 'password' ? 'eye-invisible' : 'eye'"/>
          </span>
        </a-form-model-item>
        <a-form-model-item prop="newPassword">
          <a-input
            :key="passwordType"
            ref="password"
            v-moda="moda.newPassword"
            :type="passwordType"
            placeholder="请输入新密码（长度6-20位字母和数字）"
            name="password"
            tabindex="4"
            autocomplete="on"
            @blur="capsTooltip = false"
          />
          <span class="show-pwd" @click="showPwd">
            <a-icon :type="passwordType === 'password' ? 'eye-invisible' : 'eye'"/>
          </span>
        </a-form-model-item>

        <a-tooltip v-moda="capsTooltip" content="大小写锁定开关打开" placement="right" manual>
          <a-form-model-item prop="confirm">
            <a-input
              :key="passwordType"
              ref="password"
              v-moda="moda.confirm"
              :type="passwordType"
              placeholder="请再次输入您的新密码（长度6-20位字母和数字）"
              name="password"
              tabindex="4"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
            />
            <span class="show-pwd" @click="showPwd">
              <a-icon :type="passwordType === 'password' ? 'eye-invisible' : 'eye'"/>
            </span>
          </a-form-model-item>
        </a-tooltip>
        <a-form-model-item>
          <div style="display: flex;justify-content:flex-end ">
            <a-button type="primary" @click="save">确定</a-button>
          </div>
        </a-form-model-item>
      </a-form-model>
    </a-card>
  </b-page>
</template>

<script>

  import { changePwd } from '../../../api/login'

  export default {
    name: 'UserChangePassword',
    data () {
      const validatePass = (rule, value, callback) => {
        if (value && value.trim().length !== value.length) {
          callback(new Error('密码不能包含空格'))
        } else if (!value || value.length < 6) {
          callback(new Error('请输入密码, 大于等于6位'))
        } else {
          if (this.moda.confirm !== '') {
            this.$refs.form.validateFiad('confirm')
          }
          callback()
        }
      }
      const validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入确认密码'))
        } else if (value !== this.moda.newPassword) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        passwordType: 'password',
        capsTooltip: false,
        id: null,
        visible: false,
        loading: false,
        title: '修改密码',
        rules: {
          oldPassword: [
            { required: true, message: '请输入旧密码', trigger: 'blur' }
          ],
          newPassword: [
            { required: true, message: '请输入新密码', trigger: 'blur' },
            { validator: validatePass, trigger: 'blur' }
          ],
          confirm: [
            // { required: true, message: '请确认密码', trigger: 'blur' },
            { validator: validatePass2, trigger: 'blur' }
          ]
        },
        moda: {
          oldPassword: '',
          password: '',
          confirm: ''
        }
      }
    },
    methods: {
      checkCapslock ({ shiftKey, key } = {}) {
        if (key && key.length === 1) {
          if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
            this.capsTooltip = true
          } else {
            this.capsTooltip = false
          }
        }
        if (key === 'CapsLock' && this.capsTooltip === true) {
          this.capsTooltip = false
        }
      },
      showPwd () {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      open (id) {
        this.visible = true
        this.id = id
        // this.init();
      },
      hide () {
        this.visible = false
      },
      save () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.doSave()
          } else {
            return false
          }
        })
      },
      doSave () {
        const tempPassword = this.moda.newPassword
        if (tempPassword.trim().length !== tempPassword.length) {
          this.$message.error('密码不能包含空格')
          return
        }
        this.loading++
        changePwd(this.moda.oldPassword, this.moda.newPassword).then(res => {
          this.loading--
          this.$message.success('密码更新成功，下次登录请使用新密码')
          this.moda = {
            oldPassword: '',
            password: '',
            confirm: ''
          }
        }).catch(err => {
          this.loading--
          this.$message.error(err.response.data.message)
        })
      }
    }
  }
</script>

<style lang="less">
  .user-change-password {
    width: 100%;
    background: white;
    height: 100%;
    display: flex;
    justify-content: center;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 0px;
    font-size: 16px;
    color: #a6a6a6;
    cursor: pointer;
    user-saect: none;
  }
</style>
